package logic

import (
	"context"
	"usershop/user_srv/internal/svc"
	"usershop/user_srv/model/mysql"
	"usershop/user_srv/model/redis"
	"usershop/user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type TelLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTelLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TelLoginLogic {
	return &TelLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TelLoginLogic) TelLogin(in *user.TelLoginRequest) (*user.TelLoginResMs, error) {
	// todo: 手机号登录

	//获取缓存中的短信验证码
	code := redis.GetSmsCode(in.Tel, "login")

	if code == "" {
		logx.Error("缓存中没有查询到数据.......", code)
		return nil, nil
	}

	u := mysql.User{
		Mobile: in.Tel,
	}
	err := u.TelLog()
	if err != nil {
		return nil, err
	}
	if err != nil {
		logx.Error("手机号码查询失败", err)
		return nil, err
	}

	return &user.TelLoginResMs{
		Code: code,
	}, nil
}
